﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtlity;
using Models;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class SampleService
    {
        #region 创建
        public int InsertInfo(Sample sample,string tableName)
        {
            string sql = "insert into " + tableName;
            sql += "(WorkParentId,SerialId,OrderNum,Mold,Machine,Customer,Maktx,Reason,Requirement,Department)";
            sql += " values(@WorkParentId,@SerialId,@OrderNum,@Mold,@Machine,@Customer,@Maktx,@Reason,@Requirement,@Department)";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",sample.WorkParentId),
                new SqlParameter("@SerialId",sample.SerialId),
                new SqlParameter("@OrderNum",sample.OrderNum),
                new SqlParameter("@Mold",sample.Mold),
                new SqlParameter("@Machine",sample.Machine),
                new SqlParameter("@Customer",sample.Customer),
                new SqlParameter("@Maktx",sample.Maktx),
                new SqlParameter("@Reason",sample.Reason),
                new SqlParameter("@Requirement",sample.Requirement),
                new SqlParameter("@Department",sample.Department)
            };
            return SQLHelper.Update(sql, param);
        }
        #endregion

        #region 更改
        public int UpdateInfo(Sample sample,string tableName)
        {
            string sql = "update " + tableName;
            sql += " set OrderNum=@OrderNum,Mold=@Mold,Machine=@Machine,Customer=@Customer,Maktx=@Maktx,Reason=@Reason,";
            sql += "Requirement=@Requirement,Department=@Department where WorkParentId=@WorkparentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",sample.WorkParentId),
                new SqlParameter("@SerialId",sample.SerialId),
                new SqlParameter("@OrderNum",sample.OrderNum),
                new SqlParameter("@Mold",sample.Mold),
                new SqlParameter("@Machine",sample.Machine),
                new SqlParameter("@Customer",sample.Customer),
                new SqlParameter("@Maktx",sample.Maktx),
                new SqlParameter("@Reason",sample.Reason),
                new SqlParameter("@Requirement",sample.Requirement),
                new SqlParameter("@Department",sample.Department)
            };
            return SQLHelper.Update(sql, param);
        }
        #endregion

        #region 查询
        public Sample GetInfo(int workParentId, string tableName)
        {
            string sql = "select * from" + tableName;
            sql += " where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId)
            };
            Sample sample = null;
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            if(objReader.Read())
            {
                sample = new Sample()
                {
                    Id = Convert.ToInt32(objReader["Id"]),
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    SerialId = objReader["SerialId"].ToString(),
                    OrderNum = Convert.ToInt32(objReader["OrderNum"]),
                    Mold = objReader["Mold"].ToString(),
                    Machine = objReader["Machine"].ToString(),
                    Customer = objReader["Customer"].ToString(),
                    Maktx = objReader["Maktx"].ToString(),
                    Reason = objReader["Reason"].ToString(),
                    Requirement = objReader["Requirement"].ToString(),
                    Department = objReader["Department"].ToString(),
                    Statues = objReader["Statues"].ToString()
                };
            }
            objReader.Close();
            return sample;

        }
        #endregion
    }
}
